<?php
namespace App\Repository\Db;

use App\Exceptions\ValidateException;
use App\Repository\Contracts\MemberIntegralInterface;
use App\Repository\Contracts\MemberIntegralJavaInterface;
use App\Repository\Contracts\MemberInterface;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-16 14:44
 *
 * @Description:
 */
class MemberIntegralRepository extends Repository implements MemberIntegralInterface
{
    protected $rules = [
        'created' => [
            'uid' => 'required|exists:member,uid',
            'store_id' => 'required',
            'integral' => 'required',
            'type' => 'sometimes|integer'
        ],
        'updated' => [
            'uid' => 'required|integer|exists:member,uid',
            'store_id' => 'required',
            'integral' => 'required',
            'type' => 'sometimes|integer'
        ]
    ];

    protected $messages = [
        'uid.required' => "会员 ID 不能为空",
        'uid.exists' => "会员不存在",
        'store_id.required' => "门店 ID 不能为空",
        'integral.required' => "积分不能为空",
        'type.integer' => "充值类型必须为整数"
    ];

    protected function modelName()
    {
        return 'App\Model\MemberIntegral';
    }

    public function lists($limit, $page, $map = [])
    {
        $query = $this->searchMap($map);
        $total = $query->count();
        $data = $query->orderBy('uid', 'desc')->with('integrals')->simplePaginate($limit, ['*'], 'page', $page);
        $list = [];
        foreach ($data as $key => $val) {
            $list[$key] = $val;
            $list[$key]['name'] = $val->integrals->real_name;
        }

        return $this->simplePaginate('list', $list, $total, ceil($total/$limit), $page);
    }

    public function detail($uid)
    {
        if (env('DIRECT_SALE') && env('JAVA_NETWORK')) {
            $result = $this->container->make(MemberIntegralJavaInterface::class)->findListByUid($uid);
            if ($result['resultType'] === 'success') {
                return $result['resultData'];
            }

            throw new ValidateException('当前处于离线状态,请稍后');
        }

        return $this->findAllBy('uid', $uid);
    }

    public function created(array $data)
    {
        $this->validate($data, 'created');
        /*if (env('DIRECT_SALE')) {
            $result = $this->container->make(MemberIntegralJavaInterface::class)->syncCreated($data['store_id'], $data['integral']);
            if ($result['resultType'] === 'success') {
                $this->save($data);
                return $this->container->make(MemberInterface::class)->updateIntegral($data['uid'], $data['integral']);
            }

            throw new ValidateException('当前处于离线状态,请稍后');
        }*/

        $this->save($data);
        return $this->container->make(MemberInterface::class)->updateIntegral($data['uid'], $data['integral']);
    }

    public function updated($uid, array $data)
    {
        $data['uid'] = $uid;
        $this->validate($data, 'updated');
        /*if (env('DIRECT_SALE')) {
            if ($this->container->make(MemberIntegralJavaInterface::class)->syncUpdated($uid, $data['integral'])) {
                $this->update($data, $uid, 'uid');
                return $this->container->make(MemberInterface::class)->updateIntegral($data['uid'], $data['integral']);
            }

            throw new ValidateException('当前处于离线状态,请稍后');
        }*/

        $this->update($data, $uid, 'uid');
        return $this->container->make(MemberInterface::class)->updateIntegral($data['uid'], $data['integral']);
    }
}